function [ w ] = QL( tiles, mdp)

    w=zeros(length(mdp.actions), tiles.num_features);
    Q=zeros(1,length(mdp.actions));
    num_episodios=300;
    alpha=0.1;
    lambda=0.9;
    epsilon=0.1;
    max_steps=5000;

    for episode=1:num_episodios
        e = zeros(length(mdp.actions),tiles.num_features);
        s = mdp.getRandomState(mdp);
        steps=1;
        a=randi(length(mdp.actions));
        t = getFactors(tiles, s);
        eoe=false;
        while(~eoe && steps<max_steps)          
            e(a,:) = e(a,:)+t;
            
            [s_next,r,eoe]= mdp.simularAmbiente(mdp, s,mdp.actions(a));
            delta = r - w(a,:)*t';
            t = getFactors(tiles, s_next);
           
            for i=1:length(mdp.actions)
                Q(i) = w(i,:)*t';
            end
            delta = delta + mdp.gamma * max(Q);
            w(a,:) = w(a,:) + alpha * delta * e(a,:);
            
            if rand() < epsilon
                a = randi(length(mdp.actions));
                e = zeros(length(mdp.actions),tiles.num_features);
            else
                for i=1:length(mdp.actions)
                    Q(i) = w(i,:)*t';
                end
                maximos = find(Q==max(Q));
                a = maximos(randsample(length(maximos), 1));
                e(a,:)=mdp.gamma * lambda * e(a,:);
            end
            s=s_next;
            steps=steps+1;
        end
    end